define([], function () {
    var app = angular.module('App');
    app.controller('metro.menu.headerCtrl', ['$rootScope', '$state', '$scope', 'Project', 'viewbag', 'quickbarHandler', function ($rootScope, $state, $scope, Project, viewbag, quickbarHandler) {
        /** 筛选后的项目列表 */
        $scope.filterProjects = [];

        /** 切换城市和项目类型 */
        (function () {
            $scope.typeList = Project.typeList;
            $scope.cityList = Project.cityList;
            $scope.curType = viewbag.curType;
            $scope.curCity = viewbag.curCity;

            /** 更换城市 */
            $scope.changeCity = function (city) {
                $scope.curCity = viewbag.curCity = city;
                broadcastCurCityChange();
            };

            /** 更换项目类型 */
            $scope.changeType = function (type) {
                $scope.curType = viewbag.curType = type;
                Project.getAllCityProjects(type.typeCode).then(function () {
                    $scope.curCity = viewbag.curCity;
                    broadcastCurCityChange();
                    $scope.cityList = Project.cityList;
                });
            };
        })();

        /** 发送城市变化消息 */
        function broadcastCurCityChange() {
            $rootScope.$broadcast('curCityChange');
        }

        /** 跳转到工程 */
        $scope.gotoPrj = function (prj) {
            viewbag.curPrj && (viewbag.curPrj.active = false);
            prj.active = true;
            viewbag.curPrj = prj;
            searchForm.parent().removeClass('open');
            $state.go('metro.type.project', {
                type: viewbag.curType.typeCode,
                id: prj.prjId
            });
        };

        /** 项目查找框的事件处理 */
        var searchForm = $('#project-search-form');
        /** input的处理事件 */
        (function () {
            /** 处理search input打开关闭状态 */
            $scope.toggleInput = function () {
                if (searchForm.hasClass('open')) {
                    searchForm.removeClass("open");
                    searchForm.find('.form-control').blur();
                } else {
                    searchForm.addClass("open");
                    searchForm.find('.form-control').focus();
                }
            };
            /** 处理丢失焦点 */
            $scope.inputBlur = function () {
                setTimeout(function () {
                    searchForm.parent().removeClass('open');
                }, 300);
            };
            /** 处理双击 */
            $scope.inputDoubleClick = function () {
                if ($scope.filterProjects.length === 0) return;
                searchForm.parent().addClass('open');
            }
            /** 处理下拉菜单 */
            $scope.inputKeyDown = function (e) {
                var keyCodeAllowed = [38, 40, 13];
                var dropdown = $('#filter-project-dropdown');
                if (!$scope.filterProjects.length || keyCodeAllowed.indexOf(e.keyCode) < 0) return;
                var curIndex = -1;
                $scope.filterProjects.some(function (prj, index) {
                    if (prj.hover === true) { //用来指示project上的鼠标悬停
                        curIndex = index;
                    } else return false;
                });
                var curEle, scroll, tempPrj;
                tempPrj = $scope.filterProjects[curIndex];
                if (e.keyCode === 13) {
                    tempPrj && $scope.gotoPrj(tempPrj);
                    return;
                } else {
                    if (e.keyCode === 40) { // 下
                        curIndex++;
                        if (curIndex > ($scope.filterProjects.length - 1)) return;
                        tempPrj && (tempPrj.hover = false);
                        curEle = $('#filter-project-dropdown li').eq(curIndex);
                        scroll = curEle.offset().top;
                        if ((scroll + curEle.height()) > (dropdown.height())) {
                            dropdown.scrollTop(dropdown.scrollTop() + scroll - dropdown.height() + curEle.height());
                        }
                    } else if (e.keyCode === 38) { // 上
                        curIndex--
                        if (curIndex < 0) return;
                        tempPrj && (tempPrj.hover = false);
                        curEle = $('#filter-project-dropdown li').eq(curIndex);
                        scroll = curEle.offset().top;
                        if (curEle.offset().top - 46 < 0) {
                            dropdown.scrollTop(dropdown.scrollTop() + scroll - curEle.height() - 5);
                        }
                    }
                    var prj = $scope.filterProjects[curIndex];
                    if (!prj) return;
                    prj.hover = true;

                }
            };
        })();

        //筛选选项
        $scope.filterOption = {
            name: '',
            mPrjLevel: '',
            mState: '',
            mConstructionCorp: ''
        };



        /** 用户输入工程名称来筛选项目 */
        $scope.prjNameChanged = function () {
            applyFilter();
            if ($scope.filterOption.name && $scope.filterProjects.length) searchForm.parent().addClass('open');
            else searchForm.parent().removeClass('open');
        }

        /** 执行筛选 */
        function applyFilter(para) {
            $scope.curCity.backupLines.forEach(function (line) {
                line.projects = line.backupProjects.filter(function (prj) {
                    prj.hover = false;//留给高亮选择该项目
                    var pass = true;
                    for (var key in $scope.filterOption) {
                        pass = prj[key].indexOf($scope.filterOption[key]) >= 0;
                        if (!pass) break;
                    }
                    return pass;
                })
            });
            $scope.filterProjects = [];
            $scope.curCity.lines = $scope.curCity.backupLines.filter(function (line) {
                if (line.projects.length > 0) {
                    $scope.filterProjects = $scope.filterProjects.concat(line.projects);
                    return true;
                } else {
                    return false;
                }
            });
            viewbag.curCity = $scope.curCity;//再保存到视图缓存中
            broadcastCurCityChange();
        }




    }]);
});